{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Grove Gesture Example\n",
    "\n",
    "This example shows how to use the \n",
    "[Grove gesture sensor](http://wiki.seeedstudio.com/Grove-Gesture_v1.0/) on the board.\n",
    "The gesture sensor can detect 10 gestures as follows:\n",
    "\n",
    "| Raw value read by sensor | Gesture            |\n",
    "|--------------------------|--------------------|\n",
    "| 0                        | No detection       |\n",
    "| 1                        | forward            |\n",
    "| 2                        | backward           |\n",
    "| 3                        | right              |\n",
    "| 4                        | left               |\n",
    "| 5                        | up                 |\n",
    "| 6                        | down               |\n",
    "| 7                        | clockwise          |\n",
    "| 8                        | counter-clockwise  |\n",
    "| 9                        | wave               |\n",
    "\n",
    "\n",
    "For this notebook, a PYNQ Arduino shield is also required.\n",
    "The grove gesture sensor is attached to the I2C interface on the shield. \n",
    "This grove sensor should also work with PMOD interfaces on the board."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "\n",
       "require(['notebook/js/codecell'], function(codecell) {\n",
       "  codecell.CodeCell.options_default.highlight_modes[\n",
       "      'magic_text/x-csrc'] = {'reg':[/^%%microblaze/]};\n",
       "  Jupyter.notebook.events.one('kernel_ready.Kernel', function(){\n",
       "      Jupyter.notebook.get_cells().map(function(cell){\n",
       "          if (cell.cell_type == 'code'){ cell.auto_highlight(); } }) ;\n",
       "  });\n",
       "});\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. Instantiate the sensor object"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pynq.lib.arduino import Grove_Gesture\n",
    "from pynq.lib.arduino import ARDUINO_GROVE_I2C\n",
    "\n",
    "sensor = Grove_Gesture(base.ARDUINO, ARDUINO_GROVE_I2C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2. Set speed\n",
    "There are currently 2 modes available for users to use: `far` and `near`.\n",
    "The corresponding fps are 120 and 240, respectively.\n",
    "For more information, please refer to [Grove gesture sensor](http://wiki.seeedstudio.com/Grove-Gesture_v1.0/)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "sensor.set_speed(240)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3. Read gestures\n",
    "The following code will read 10 gestures within 30 seconds. \n",
    "Try to change your gesture in front of the sensor and check the results."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "no-detection\n",
      "no-detection\n",
      "left\n",
      "no-detection\n",
      "no-detection\n",
      "no-detection\n",
      "no-detection\n",
      "down\n",
      "no-detection\n",
      "no-detection\n"
     ]
    }
   ],
   "source": [
    "from time import sleep\n",
    "\n",
    "for i in range(10):\n",
    "    print(sensor.read_gesture())\n",
    "    sleep(3)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
